package control;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.InfoManageDao;
import dao.SurveyContentDao;
import dao.SurveyDetailDao;
import entity.InfoManage;
import entity.SurveyContent;
import entity.SurveyDetail;

/**
 * Servlet implementation class GetData
 */
@WebServlet("/GetData")
public class GetData extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetData() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//中文乱码五星好评
		Integer major_Id = Integer.valueOf(request.getParameter("majorname")); // 获取专业id
		Integer courses_Id = Integer.valueOf(request.getParameter("coursesname")); // 获取课程id
		Integer teacher_Id = Integer.valueOf(request.getParameter("teachername")); // 获取教师id
//		System.out.println("....>"+"专业："+major_Id+"，课程："+courses_Id+",教师："+teacher_Id);
		String date = request.getParameter("subdate"); // 获取时间
	    Calendar now =Calendar.getInstance();
		String year=String.valueOf(now.get(Calendar.YEAR));
		String month=String.valueOf(now.get(Calendar.MONTH));
		String day=String.valueOf(now.get(Calendar.DATE));	
		
		String periods="HXZY-YNKM-"+year+"/"+month+"/"+day;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 定义时间格式（跟Hbuilder输出的时间格式要一样）
		Date date_uti = null;
		try {
			date_uti = sdf.parse(date); // 把date强转成 Date 类型
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SurveyDetail sdt = new SurveyDetail();
		SurveyDetailDao sdd = new SurveyDetailDao();
		SurveyContentDao scd = new SurveyContentDao();
		InfoManageDao imd=new InfoManageDao();
		InfoManage im=new InfoManage();
		List<SurveyContent> sc = scd.getAll(); // 获取项目细节
		List<InfoManage> im_list=imd.getAll();
   		String[] scores = request.getParameterValues("score"); // 获取分数
		
   	    //把数据保存进infomanage这个表里 
         String suggest=request.getParameter("suggest");   
         String session_id=request.getSession().getId();  //给评价的学生一个编号
         im.setSession_id(session_id);
         im.setSuggest(suggest);     //把建议内容添加到suggest里面   
		 im.setCourses_Id(courses_Id);    
		 im.setDate(date_uti);
	     im.setTeacher_Id(teacher_Id);
	     Integer info_Id=(im_list.get(im_list.size()-1).getId())+1;  //把infomanage的id设为SurveyDetail的外键
	     System.out.println("....>"+session_id+suggest);

		
		int sum=0;
		for (int i = 0; i < sc.size(); i++) {
			sdt.setContent_Id(sc.get(i).getId());
			sdt.setInquiryData(date_uti); // 把时间添加进保存数据的表的对象里
			sdt.setPeriods(periods);          //设期数
			sdt.setMajor_Id(major_Id);
			sdt.setCourses_Id(courses_Id);
			sdt.setTeacher_Id(teacher_Id);
			sdt.setInfo_Id(info_Id);
			if (scores[i].equals("")) {
				sdt.setScore(5);
			} else {
				sdt.setScore(Integer.valueOf(scores[i]));				
			}
			sdd.insert(sdt);
			sum+=sdt.getScore();
		}
	      im.setScore_Num(sum);
		  imd.insert(im);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
